package com.aaa.three.pay;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * @PACKAGE_NAME: com.aaa.three.pay
 * @NAME: OutPay
 * @USER: Tprairie
 * @DATE: 2022/2/26
 * @PROJECT_NAME: three
 */
public class OutPay {
    private static final Logger logger = LoggerFactory.getLogger(Alipay.class);
    private static final String format = "json";
    private  String appId="2021000119621327";
    private  String privateKey="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDDvLRn+ARyPhiZeAraBMwKZOq6xiLCCow5PoNpmGpu9FroWHIBLHONBz/mvjBdsQec8BMKp+IkY1N9Z3iCO91/nVLimqY+oipfiBgydKmjFyl8hamG6DGf2riArJ5dMCCRpEMfQyMN2eEx19SHrRzScDj4NgVhwEzxQnB3i/cKkSNANurG46Ryyyo2Hhs0LGbwt53axwEH9OgsOqCI9F86pDDk0s8JSbnmv3qPMVv0n+qInqxb9EgnJTOpIbdtnSGIvcAugSeoQU7ov2aD32PXytPxieM2IbeVL7nPnY3QTK6BvpxG7YH6GdsTsORW61HgpbCXlUtiXtmRhkysAi4bAgMBAAECggEAIWEEwtYgq/q65zD5aMR4py0eektfkgeKchifAFrli5ZQCl9K0bhq3BnoY8j7Jn8R9Ulbde27OslVa4VBn7PlyeViRRJqVzQAVdo3hiMu6okAoiOJDWB7Ve9Ol2LYfru+UhfY+nPQfoQxSvarX5e92oUYsftEX68Tb/EMjNZpgnuBVe2kyQxtvMKBHUgPzTpXlWKzldWpZ/9c+NwwA8HLUR8wH071KI3ksX4SJeOEmPgY9VsTPDfVFrlojO6oZZXoaPFRBu810kOFl61skqhMjK/fUoOSRpJtEGsIpopLiAtZ+bM13Cv8dacUe7yHvocgKBwucwD4GWdUYKK8T5DCwQKBgQDhnnnNSl5yfD0zFZ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Lj3SagRzJsJtNK4O8kvMjoXqFYiLu4i5i+XSics8KpD90J829On2DObPLb/s3354M6KX0gGwBvqIZLD+bKppUmfqyGGxX6sPRcnDfc0ZjJHLZAB41JVYmZZTajcBmDQ/LzisHNna8STGy2NAICjDmuDztdH30SDQIDAQAB";
    private  String notifyUrl="http://localhost:8080/demo/doctor/erro.html";
    private  String returnUrl="http://localhost:8080/demo/userr/userResult.html";
    private  String signType="RSA2";
    private  String charset="utf-8";
    private  String gatewayUrl="https://openapi.alipaydev.com/gateway.do";
    public static void out(Integer id, Integer price, HttpServletResponse response)throws Exception{
        // 设置编码格式
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient( Alipay.gatewayUrl, Alipay.appId, Alipay.privateKey, Alipay.format, Alipay.charset, Alipay.publicKey, Alipay.signType);
        //设置请求参数
        AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest();
        //商户订单号，必填
        String out_trade_no = new String(String.valueOf(id));
        //需要退款的金额，该金额不能大于订单金额，必填
        String refund_amount = new String(String.valueOf(price));
        //标识一次退款请求，同一笔交易多次退款需要保证唯一。如需部分退款，则此参数必传；不传该参数则代表全额退款
        String out_request_no = new String(UUID.randomUUID().toString());
        // 字符转义很重要
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"refund_amount\":\""+ refund_amount +"\","
                + "\"out_request_no\":\""+ out_request_no +"\"}");
        //请求
        alipayClient.execute(alipayRequest).getBody();
        response.sendRedirect("http://localhost:8080/demo/userr/userResult.html");
    }
}
